package data;

import java.util.ArrayList;

import org.w3c.dom.Node;

public class RTENode implements Comparable<RTENode>{
	public static int nodeCount;
	
	private String id = "";
	private String word = "";
	private String lemma = "";
	private String posTag = "";
	private String relationId = "";
	private String relation = "";
	private String antdecendant = "";
	private int modifiedId;
	
	private ArrayList<RTENode> children;
	
	private boolean used = false;
	
	/**
	 * Constructor for creating test trees
	 */
	public RTENode(String nodeId, String relationId, String lemma) {
		this.id = nodeId;
		this.relationId = relationId;
		this.lemma = lemma;
		this.modifiedId = Integer.parseInt(id);
		this.children = new ArrayList<RTENode>();
	}

	public RTENode(String nodeId, Node word, Node lemma, Node posTag, Node relation, Node antecendent) {
		nodeCount++;
		this.id = nodeId.toLowerCase();
		if(word != null) 
			this.word = word.getTextContent().toLowerCase();
		if(lemma != null)
			this.lemma = lemma.getTextContent().toLowerCase();
		if(posTag != null)
			this.posTag = posTag.getTextContent().toLowerCase();
		if(relation != null) {
			Node relationNode = relation.getAttributes().getNamedItem("parent");
			if(relationNode != null)
				relationId = relationNode.getTextContent().toLowerCase();
		}
		if(relation != null) 
			this.relation = relation.getTextContent();
		if(antecendent != null) 
			this.antdecendant = antecendent.getTextContent();

		this.word = this.word.replace("\n", "");
		this.word = this.word.replace("\t", "");
		this.lemma = this.lemma.replace("\n", "");
		this.lemma = this.lemma.replace("\t", "");
		this.posTag = this.posTag.replace("\n", "");
		this.posTag = this.posTag.replace("\t", "");
		this.relationId = this.relationId.replace("\n", "");
		this.relationId = this.relationId.replace("\t", "");
		this.relation = this.relation.replace("\n", "");
		this.relation = this.relation.replace("\t", "");
		this.antdecendant = this.antdecendant.replace("\n", "");
		this.antdecendant = this.antdecendant.replace("\t", "");
		
		this.modifiedId = nodeCount;
		
		this.children = new ArrayList<RTENode>();
	}
	
	public void addChild(RTENode node) {
		this.children.add(node);
	}
	
	public static RTENode findNode(ArrayList<RTENode> nodes, String id) {
		if(id.charAt(0) == 'e')
		{
			for(int i = nodes.size() - 1; i >= 0; i--) {
				if(nodes.get(i).getId().equals(id))
					return nodes.get(i);
			}
		}
		else {
			int idVal = Integer.valueOf(id);
			return nodes.get(idVal-1);
		}
		System.err.println("Couldn't find node in TreeNode.findNode: " + id);
		return null;
	}
	

	public String getId() {
		return id;
	}

	public String getWord() {
		return word;
	}

	public String getLemma() {
		return lemma;
	}
	
	public void setLemma(String value) {
		this.lemma = value;
	}

	public String getPosTag() {
		return posTag;
	}

	public String getRelationId() {
		return relationId;
	}

	public String getRelation() {
		return relation;
	}
	
	public String getAntdecendant() {
		return this.antdecendant;
	}
	
	public RTENode getLeftChild() {
		if(children.size() > 0)
			return children.get(0);
		return null;
	}

	public ArrayList<RTENode> getChildren() {
		return this.children;
	}
	
	public int getModifiedId() {
		return modifiedId;
	}

	@Override
	public int compareTo(RTENode o) {
		if(this.getModifiedId() > o.getModifiedId())
			return 1;
		else if(this.getModifiedId() < o.getModifiedId())
			return -1;
		else return 0;
	}

	public void setModifiedId(int id) {
		this.modifiedId = id;
		
	}

	public boolean isUsed() {
		return used;
	}

	public void setUsed(boolean used) {
		this.used = used;
	}
}
